package org.kl.auditsupervision.service.fnd;

 

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.kl.auditsupervision.entity.fnd.ZgCs;
import org.kl.auditsupervision.repository.fnd.ZgCsDao;
import org.kl.bf.entity.BusinessEntity;
import org.kl.bf.service.BasicService;
import org.kl.bf.utils.BasicConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
import org.springside.modules.utils.Collections3;

import com.alibaba.fastjson.JSON;
 
@Component
// 默认将类中的所有public函数纳入事务管理.
@Transactional
public class ZgCsService extends BasicService<ZgCs> {
	@Autowired
	private ZgCsDao zgCsDao;
	
	public List<ZgCs> findByAuditProDtlId(String auditProDtlId){
		return zgCsDao.findByAuditProDtlId(auditProDtlId);
	}
	
	public List<ZgCs> findByAuditProDtlIdOrderByZgPlanTimeDesc(String auditProDtlId){
		return zgCsDao.findByAuditProDtlIdOrderByZgPlanTimeDesc(auditProDtlId);
	}
	
	
	
	public void save(ZgCs entity, @RequestParam(value = "inserted") String detailInserted,
			@RequestParam(value = "updated") String detailUpdated, @RequestParam(value = "deleted") String detailDeleted) {
		zgCsDao.save(entity);
		if (StringUtils.isNotBlank(detailInserted)) {
			List<ZgCs> inserts = JSON.parseArray(detailInserted, ZgCs.class);
			if (Collections3.isNotEmpty(inserts)) {
				for (ZgCs a : inserts) {
					// 增加与主表相关的数据复制代码
					zgCsDao.save(a);
				}
			}
		}
		if (StringUtils.isNotBlank(detailUpdated)) {
			List<ZgCs> updates = JSON.parseArray(detailUpdated, ZgCs.class);
			if (Collections3.isNotEmpty(updates)) {
				for (ZgCs b : updates) {
					zgCsDao.save(b);
				}
			}
		}
		if (StringUtils.isNotBlank(detailDeleted)) {
			List<ZgCs> deletes = JSON.parseArray(detailDeleted, ZgCs.class);
			if (Collections3.isNotEmpty(deletes)) {
				zgCsDao.delete(deletes);
			}
		}
	}
	
	
	public String multiDeleteZRexe(String ids) {
		String[] idArray = StringUtils.split(ids, ",");
		int i = 0,j=0;
		for (String id : idArray) {
			if (StringUtils.isNotBlank(id)) {
				ZgCs entity = basicDao.findOne(id);
				if(entity.getStatus()!=null&&entity.getStatus().equals("1")){
					j++;
				}else{
					basicDao.delete(id);
					i++;
				}
				
			}
		}
		return "成功删除" + i + "条记录,有"+j+"条记录为已审批整改措施，不可删除";
	}
}
